package com.lanlian.cayman.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.activity.ProfitLossActivity;
import com.lanlian.cayman.activity.ProfitLossSetupActivity;
import com.lanlian.cayman.adapter.ProfitLossSeniorAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.PositionDetailsInfo;
import com.lanlian.commonlib.bean.ProfitLossInfo;
import com.lanlian.commonlib.bean.ReqProfitLossList;
import com.lanlian.commonlib.bean.RespProfitLossList;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.RecyclerViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/16 10:17
 *     desc   : 止盈止损操作高级TAB
 *     version: 1.0
 * </pre>
 */
public class ProfitLossSeniorFragment extends BaseFragment {

    private RecyclerView mRecycleView;
    private SmartRefreshLayout mLayRefresh;

    private PositionDetailsInfo mPositionDetailsInfo;
    private ArrayList<ProfitLossInfo> mList = new ArrayList<>();
    private ProfitLossSeniorAdapter mAdapter;
    private View footView;

    public static ProfitLossSeniorFragment getInstance(PositionDetailsInfo positionDetailsInfo) {
        ProfitLossSeniorFragment fragment = new ProfitLossSeniorFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("positionDetailsInfo", positionDetailsInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profit_loss_senior, null);
        initData();
        initView(view);
        setListeners();
        initAdapter();
        mLayRefresh.autoRefresh();
        return view;
    }

    @Override
    protected void initData() {
        super.initData();
        mPositionDetailsInfo = (PositionDetailsInfo) getArguments().getSerializable("positionDetailsInfo");
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mLayRefresh = view.findViewById(R.id.lay_refresh);
        mRecycleView = view.findViewById(R.id.recycle_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getProfitLossList();
            }
        });
    }

    private void initAdapter() {
        View headView = getActivity().getLayoutInflater().inflate(R.layout.head_view_profit_loss_senior, null);
        footView = getActivity().getLayoutInflater().inflate(R.layout.foot_view_profit_loss_senior, null);
        mAdapter = new ProfitLossSeniorAdapter(R.layout.adapter_profit_loss_senior, mList);
        mRecycleView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecycleView.setLayoutManager(linearLayoutManager);
        mRecycleView.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.VERTICAL, R.drawable.divider));
        mAdapter.setHeaderView(headView);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), ProfitLossSetupActivity.class)
                        .putExtra("isNew", false)
                        .putExtra("isOrdinary", false)
                        .putExtra("positionDetailsInfo", mPositionDetailsInfo)
                        .putExtra("profitLossInfo", mList.get(position)));
            }
        });
    }

    private void getProfitLossList() {
        ReqProfitLossList reqProfitLossList = new ReqProfitLossList();
        reqProfitLossList.setHoldingId(mPositionDetailsInfo.getlPositionNo());
        VolleyRequestUtils.getInstance().getProfitLossList(this.toString(), reqProfitLossList, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mLayRefresh.finishRefresh();
                RespProfitLossList respProfitLossList = (RespProfitLossList) response;
                mList = respProfitLossList.getDataList();
                mAdapter.setNewData(mList);
                if (mList.size() == 0) {
                    mAdapter.removeFooterView(footView);
                } else {
                    mAdapter.setFooterView(footView);
                }
                ProfitLossActivity.mAmount = mPositionDetailsInfo.getlCurrentAmount();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                ProfitLossActivity.mAmount = mPositionDetailsInfo.getlCurrentAmount();
                mLayRefresh.finishRefresh();
                showToast(baseResponse.getMsg());
            }
        });
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        switch (event.getCode()) {
            case EventCode.EVENT_PROFITLOSS_REFRESH:
                mLayRefresh.autoRefresh();
                break;
            default:
                break;
        }
    }
}
